package org.javaweb.cms_web.controller;

import org.javaweb.cms_web.model.Achievement;
import org.javaweb.cms_web.model.Team;
import org.javaweb.cms_web.model.User;
import org.javaweb.cms_web.service.AchievementService;
import org.javaweb.cms_web.service.TeamService;
import org.javaweb.cms_web.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/achievements")
public class AchievementDTOController {

    @Autowired
    private AchievementService achievementService;

    @Autowired
    private TeamService teamService;

    @Autowired
    private UserService userService;

    // 显示科研成果列表页面
    @GetMapping("/list/{teamID}")
    public String showAchievementList(
            @PathVariable("teamID") int teamID,
            @RequestParam(value = "search", required = false) String search,
            @RequestParam(value = "category", required = false) Integer category,
            Model model) {

        List<Achievement> achievements;

        if (search != null && !search.isEmpty()) {
            // 进行模糊查询
            achievements = achievementService.getAchievementsByTeamAndTitle(teamID, search, category);
        } else {
            achievements = achievementService.getAchievementsByTeam(teamID, category);
        }

        model.addAttribute("achievements", achievements);
        model.addAttribute("teamID", teamID); // 传递团队 ID

        return "achievement-list"; // 返回科研成果列表页面
    }

    // 显示科研成果详情页面
    @GetMapping("/{achievementID}/details")
    public String showAchievementDetails(
            @PathVariable("achievementID") int achievementID,
            Model model) {

        Achievement achievement = achievementService.getAchievementById(achievementID);
        if (achievement != null) {
            Team team = teamService.getTeamById(achievement.getTeamId());
            User user = userService.findUserById(achievement.getUserId());

            // 传递团队ID
            int teamID = team.getTeamId();
            model.addAttribute("teamID", teamID);

            // 假设 article 是你的文章对象
            String AttachmentLink = achievement.getAttachmentLink();
            String encodedAttachmentLink = achievementService.prepareUrl(achievement.getAttachmentLink());
            System.out.println("编码前:" + achievement.getAttachmentLink());
            achievement.setAttachmentLink(encodedAttachmentLink);
            System.out.println("编码后:" + encodedAttachmentLink);
            // 将文章、团队和用户信息传递到模型
            model.addAttribute("AttachmentLink", AttachmentLink);

            // 将成果、团队和用户信息传递到模型
            model.addAttribute("achievement", achievement);
            model.addAttribute("team", team);
            model.addAttribute("user", user);
        }

        return "achievement-details"; // 返回科研成果详情页面
    }
}
